﻿using CLVisionMQTT;
using MQTTnet;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TestDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        MQTTClient mQTTCaller;
        ProcessMESData processMESData = new ProcessMESData();
        private void buttonInit_Click(object sender, EventArgs e)
        {
            mQTTCaller = new MQTTClient();
            mQTTCaller.EventReceivedMessage += (mes) =>
            {
                this.Invoke(new Action(() => { richTextBox1.AppendText("收到数据：" + mes + "\r\n"); }));

            };
            mQTTCaller.EventDisconnected += (mes) =>
            {
                this.Invoke(new Action(() => { richTextBox1.AppendText("收到数据：" + mes + "\r\n"); }));

            };
            //  mQTTCaller.IntMqttClient("broker.hivemq.com", 1883, "", "", "test/topic", out int ErrorCode, out string ErrorText);
            mQTTCaller.IntMqttClient("172.16.50.224", 27011, "admin", "admin", "eics/report", "123456/notify", out int ErrorCode, out string ErrorText);
            //mQTTCaller.IntMqttClient("172.16.50.224", 27011, "admin", "admin", "eics/report", "123456/notify", out int ErrorCode, out string ErrorText);
            //mQTTCaller.IntMqttClient("127.0.0.1", 27011, "admin", "admin", "eics/report", "123456/notify", out int ErrorCode, out string ErrorText);
            if (ErrorCode == -1)
            {
                MessageBox.Show(ErrorText);
                return;
            }
            else
            {
            richTextBox1.AppendText("MQTT Client Initialized Successfully!" +"\r\n"); 
            
            }

        }

        private void buttonPublish_Click(object sender, EventArgs e)
        {
            bool res = mQTTCaller.PublishData("eics/report", mesData, MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce, out int ErrorCode, out string ErrorText);
            if (ErrorCode == -1)
            {
                MessageBox.Show(ErrorText);
            }
            richTextBox1.AppendText("发送数据：" + mesData + "\r\n"); 

        }

        private void buttonDeInit_Click(object sender, EventArgs e)
        {
            mQTTCaller.DisconnectMqttClient(out int ErrorCode, out string ErrorText);
        }

        string mesData;
        private void button1_Click(object sender, EventArgs e)
        {
            var res = processMESData.CdatToMesJson(@"C:\Users\hao.chen\Desktop\TestTelegramSetting.cdat", out int ErrorCode, out string ErrorText);
            mesData = res.ToString();
            processMESData.SaveJsonToFile(@"C:\Users\hao.chen\Desktop\TestTelegramSetting111.cdat", res, out _, out _);
        }



        private void button3_Click(object sender, EventArgs e)
        {

            //MqttTopicFilter topicFilter = new MqttTopicFilter();  //"123456/notify"
            //topicFilter.Topic = "123456/notify";

            mQTTCaller.SubscribeTopic(out _, out _, "123456/notify");
        }
    }

}
